package org.linlinjava.litemall.wx.controller;

import io.swagger.annotations.*;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.ResponseUtil.Response;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.wx.annotation.LogAnno;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.dto.WxOrderStatusDTO;
import org.linlinjava.litemall.wx.service.WxOrderService;
import org.linlinjava.litemall.wx.vo.order.OrderSubmitReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;


@Api(value="订单controller",tags={"订单操作API"})
@RestController
@RequestMapping("/wx/order")
@Validated
public class WxOrderController {

    @Autowired
    private WxOrderService wxOrderService;

    @PostMapping("submit")
    public Object submit(@LoginUser Integer userId,@Validated @RequestBody OrderSubmitReqVO reqVO) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return wxOrderService.submitOrder(userId, reqVO);
    }


    @ApiOperation("订单列表")
    @GetMapping(value = "list")
    public Response list(@LoginUser Integer userId,
                       @RequestParam(defaultValue = "0") Integer showType,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return wxOrderService.list(userId, showType, page, limit, sort, order);
    }

    @ApiOperation("订单列表->状态")
    @PostMapping("orderStatusS")
    @LogAnno
    public ResponseUtil.Response orderStatusS(
            @LoginUser Integer userId,
            @RequestBody WxOrderStatusDTO wxOrderStatusDTO
    ){
        return wxOrderService.orderStatusS(userId,wxOrderStatusDTO);
    }

    @ApiOperation(value="查询订单详情")
    @GetMapping("detail")
    public Response detail(@LoginUser Integer userId, @NotNull Integer orderId) {
        return wxOrderService.detail(userId, orderId);
    }






    /**
     * 取消
     * 取消订单
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 取消订单操作结果
     */
    @PostMapping("cancel")
    @LogAnno
    public Object cancel(@LoginUser Integer userId, @RequestBody String body) {
        return wxOrderService.cancel(userId, body);
    }

    /**
     * 提前支付
     * 付款订单的预支付会话标识
     *
     * @param userId  用户ID
     * @param body    订单信息，{ orderId：xxx }
     * @param request 请求
     * @return 支付订单ID
     */
    @PostMapping("prepay")
    @LogAnno
    public Object prepay(@LoginUser Integer userId, @RequestBody String body, HttpServletRequest request) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return wxOrderService.prepay(userId, body, request);
    }

    /**
     * 付款通知
     * 微信付款成功或失败回调接口
     * <p>
     * TODO
     * 注意，这里pay-notify是示例地址，建议开发者应该设立一个隐蔽的回调地址
     *
     * @param request  请求内容
     * @param response 响应内容
     * @return 操作结果
     */
    @PostMapping("pay-notify")
    @LogAnno
    public Object payNotify(HttpServletRequest request, HttpServletResponse response) {
        return wxOrderService.payNotify(request, response);
    }

    /**
     * 退款
     * 订单申请退款
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单退款操作结果
     */
    @PostMapping("refund")
    public Object refund(@LoginUser Integer userId, @RequestBody String body) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return wxOrderService.refund(userId, body);
    }

    /**
     * 确认
     * 确认收货
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("confirm")
    @LogAnno
    public Object confirm(@LoginUser Integer userId, @RequestBody String body) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return wxOrderService.confirm(userId, body);
    }

    /**
     * 删除
     * 删除订单
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("delete")
    @LogAnno
    public Object delete(@LoginUser Integer userId, @RequestBody String body) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return wxOrderService.delete(userId, body);
    }

    /**
     * 货物
     * 待评价订单商品信息
     *
     * @param userId       用户ID
     * @param orderGoodsId 订单商品id
     * @return 待评价订单商品信息
     */
    @ApiOperation(value="查询待评价订单商品信息")
    @GetMapping("goods")
    @LogAnno
    public Object goods(@LoginUser Integer userId,
    		 @ApiParam(name="orderGoodsId",value="待评价的订单商品id",required=true) @RequestParam Integer orderGoodsId) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return wxOrderService.goods(userId, orderGoodsId);
    }

    /**
     * 评论
     * 评价订单商品
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("comment")
    @LogAnno
    public Object comment(@LoginUser Integer userId, @RequestBody String body) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return wxOrderService.comment(userId, body);
    }

}
